﻿Public Class MyGridCell
    Inherits Border

    Private m_eventcount As Integer = 0
    Private m_original_child As UIElement
    Private m_message_text As TextBox
    Private m_enlarge_button As Button

    Private m_major_content As UIElement
    Private m_major_shown As Boolean = False
    Private m_minor_shown As Boolean = False

    Public Shared ReadOnly MinorContentProperty As DependencyProperty = _
        DependencyProperty.Register( _
            "MinorContent", _
            GetType(UIElement), _
            GetType(MyGridCell), _
            New FrameworkPropertyMetadata( _
                Nothing, _
                FrameworkPropertyMetadataOptions.SubPropertiesDoNotAffectRender, _
                New PropertyChangedCallback(AddressOf OnMinorContentPropertyChanged)))

    Public Property MinorContent() As UIElement
        Get
            Return GetValue(MinorContentProperty)
        End Get
        Set(ByVal value As UIElement)
            SetValue(MinorContentProperty, value)
        End Set
    End Property

    Private Shared Sub OnMinorContentPropertyChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
    End Sub

    Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Input.MouseButtonEventArgs)
        m_eventcount += 1
        If e.ClickCount = 2 Then
            EnlargeMe()
            m_message_text.Text = "OnMouseDown: doubleclick: " & m_eventcount & " major: " & IsMajor
            e.Handled = True
        Else
            m_message_text.Text = "OnMouseDown: " & m_eventcount & " major: " & IsMajor
            e.Handled = False
        End If
    End Sub

    Private Sub EnlargeMe()
        If Not IsMajor Then MakeMeMajor()
    End Sub

    Private Function MakeMeMajor() As MyGridCell
        Dim g As Grid = CType(Me.Parent, Grid)
        If g Is Nothing Then Return Nothing

        Dim oldmajor As MyGridCell = Nothing
        For Each cell As MyGridCell In g.Children.OfType(Of MyGridCell)()
            If Grid.GetRow(cell) = 0 Then oldmajor = cell
        Next
        If oldmajor Is Nothing Then Return Nothing

        ' swap cell content

        Dim oldrow As Integer = Grid.GetRow(Me)
        Dim oldcol As Integer = Grid.GetColumn(Me)

        Grid.SetRow(Me, 0)
        Grid.SetColumn(Me, 0)
        Grid.SetColumnSpan(Me, 2)

        If oldmajor IsNot Nothing Then
            Grid.SetRow(oldmajor, oldrow)
            Grid.SetColumn(oldmajor, oldcol)
            Grid.SetColumnSpan(oldmajor, 1)
        End If

        SetContent(True)
        oldmajor.SetContent(False)

        Return oldmajor

    End Function

    Private Sub SetContent(ByVal showmajor As Boolean)
        If MinorContent IsNot Nothing Then
            If showmajor Then
                m_major_content.Visibility = Windows.Visibility.Visible
                MinorContent.Visibility = Windows.Visibility.Collapsed
                m_enlarge_button.Visibility = Windows.Visibility.Collapsed
            Else
                m_major_content.Visibility = Windows.Visibility.Collapsed
                MinorContent.Visibility = Windows.Visibility.Visible
                m_enlarge_button.Visibility = Windows.Visibility.Visible
            End If
        End If
        Me.SetVisual()
        Me.InvalidateVisual()
    End Sub

    Private Sub EnlargeButtonClicked(ByVal sender As Object, ByVal e As RoutedEventArgs)
        EnlargeMe()
    End Sub

    Private Sub MyGridCell_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Initialized
        m_message_text = New TextBox
        m_message_text.Text = "initialized..."
        m_message_text.Visibility = Windows.Visibility.Collapsed

        m_enlarge_button = New Button()
        Dim run As New Run
        run.FontFamily = New FontFamily("Wingdings 3")
        run.Text = "" ' up arrow
        'm_enlarge_button.Width = 30
        m_enlarge_button.Content = run
        m_enlarge_button.Background = System.Windows.Media.Brushes.AliceBlue

        AddHandler m_enlarge_button.Click, AddressOf EnlargeButtonClicked

        m_original_child = Me.Child
        m_major_content = Me.Child
        Me.Child = Nothing

        Dim dp As New DockPanel
        dp.Children.Add(m_message_text)
        DockPanel.SetDock(m_message_text, Dock.Bottom)
        dp.Children.Add(m_enlarge_button)
        DockPanel.SetDock(m_enlarge_button, Dock.Top)
        Dim wrap As New WrapPanel
        wrap.Children.Add(m_original_child)
        If MinorContent IsNot Nothing Then wrap.Children.Add(MinorContent)
        dp.Children.Add(wrap)

        'Dim insideborder As New Border
        'insideborder.BorderBrush = Brushes.Green
        'insideborder.BorderThickness = New Thickness(2)
        'insideborder.Child = dp
        'Me.Child = insideborder
        Me.Child = dp

        Me.SetContent(IsMajor())

    End Sub

    Private ReadOnly Property IsMajor() As Boolean
        Get
            Return Grid.GetRow(Me) = 0
        End Get
    End Property

    Private Sub SetVisual()
        'If IsMajor Then
        '    Me.BorderThickness = New Thickness(3, 10, 3, 3)
        '    Me.BorderBrush = Brushes.Red
        '    Me.Background = Brushes.Red
        'Else
        '    Me.BorderThickness = New Thickness(3, 10, 3, 3)
        '    Me.BorderBrush = Brushes.Yellow
        '    Me.Background = Brushes.Yellow
        'End If
    End Sub
End Class
